from fastapi import FastAPI
from tortoise import Tortoise
from tortoise.contrib.fastapi import register_tortoise
from dotenv import load_dotenv
import os

# 加载 .env 文件
load_dotenv()

# 配置 Tortoise ORM
TORTOISE_ORM = {
    "connections": {"base": os.getenv("DATABASE_URL", "sqlite://db.sqlite3")},
    "apps": {
        "base": {"models": ["models.base", "aerich.models"], "default_connection": "base"},
    },
    'use_tz': False,
    'timezone': 'Asia/Shanghai'
}

async def init_db():
    await Tortoise.init(config=TORTOISE_ORM)
    # await Tortoise.generate_schemas()

async def close_db():
    await Tortoise.close_connections()